<template>
    <div  class="login-container">
      <div  class="login-form-wrap">
     <!-- logo -->
     <div class="login-head">
        <div class="logo"></div>
      </div>
     <!-- 表单 -->
     <el-form ref="refForm"   class="login-form"  :model='form' :rules="rules">
     <!-- 用户名 -->
       <el-form-item  prop='mobile'>
    <el-input  v-model="form.mobile"></el-input>
  </el-form-item>
  <!-- 验证码 -->
    <el-form-item prop='code' >
    <el-input  v-model="form.code"></el-input>
  </el-form-item>
  <!-- agree -->
   <el-form-item  prop='isAgree'>
          <el-checkbox v-model="form.isAgree" label="我已阅读并同意用户协议和隐私条款" name="type"></el-checkbox>
        </el-form-item>
  <!-- 登录按钮 -->
   <el-form-item>
     <el-button  type="primary" @click.prevent='aLogin'
     :loading='isLoading'  class="login-btn">登录</el-button>
  </el-form-item>
  </el-form>
      </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单验证规则
      rules: {
        // 手机号码验证
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        // 密码验证
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码', trigger: 'blur' }
        ],
        // agree表单验证
        isAgree: [
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('同意后才能登陆'))
              }
            },
            trigger: 'change'
          }
        ]
      },
      isLoading: false,
      form: {
        // 手机号
        mobile: '13911111111',
        //  密码
        code: '246810',
        isAgree: true
      }
    }
  },
  cread () {
    console.log(this)
  },
  methods: {
    async verify () {
      // 收集表单信息
      const { mobile, code } = this.form

      // 通过axios发送ajax请求
      // 正在请求
      this.isLoading = true
      try {
        const res = await this.$http({
          method: 'POST',
          url: 'mp/v1_0/authorizations',
          data: {
            mobile,
            code
          } // 成功与否弹出提示
        })
        this.$message.success('登录成功')
        this.isLoading = false// 请求结束
        localStorage.setItem('tokenStr', res.data.data.token)
        this.$router.push('/')
      } catch (err) {
        console.log(err)
        this.$message.error('错了哦，这是一条错误消息')
        this.isLoading = false// 请求结束
      }
    },
    aLogin () {
      this.$refs.refForm.validate(valid => {
        console.log('验证结果是', valid)
        if (valid) {
          this.verify()
        }
      })
    }
  }
}
</script>
<style scoped lang='less'>
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
